package com.su.edu.infra.repository.impl;

import com.su.edu.domain.model.UserDo;
import com.su.edu.infra.repository.UserRepository;
import com.su.edu.infra.repository.converter.UserPoConverter;
import com.su.edu.infra.repository.entity.User;
import com.su.edu.infra.repository.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author wangjun
 * @since 2024/6/5 14:30
 */
@Service
public class UserRepositoryImpl implements UserRepository {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserPoConverter userPoConverter;

    @Override
    public UserDo findById(Long id) {
        User user = userMapper.selectById(id);
        return userPoConverter.toDo(user);
    }

    @Override
    public void save(UserDo userDo) {
        User user = userPoConverter.toPo(userDo);
        userMapper.insert(user);
        userDo.setId(user.getId());
    }

    @Override
    public void update(UserDo userDo) {
        User user = userPoConverter.toPo(userDo);
        userMapper.updateById(user);
    }
}
